package com.yb.bussiness.shoper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgBrandMember;
import com.yb.util.comm.DateTimeUtil;

/**
 * 拼抢记录
 * 
 * @author Administrator
 * 
 */
public class TgBrandMemberServiceImpl implements TgBrandMemberService {

	private IBaseDao<TgBrandMember, Long> dao;

	public TgBrandMemberServiceImpl() {
		dao = new BaseDao<TgBrandMember, Long>(TgBrandMember.class);
	}
	
	
	 
	 
	
	/*
	 * 查询信息 @param parameMap @return
	 */
	public List<TgBrandMember> findByParameMap(Map<String, String> parameMap) {
		String str = "SELECT t.id AS id, t.brandId AS brandId , t.email AS email ," +
				"t.name AS NAME, t.telephoe AS telephoe,t.tel AS tel ," +
				"t.joinDate AS joinDate, s.name AS CODE,t.type AS TYPE," +
				"g.name AS qq,mem.sex sex " +
				"FROM  t_g_brand_member t,t_g_goods g,t_g_brand s,t_g_brand mem WHERE  t.brandId=g.id AND g.brandId = s.id  AND t.userId = mem.sysuserid";
		if (parameMap.get("brandid") != null&&!"".equals(parameMap.get("brandid"))) {
			str += " and t.brandId =" + parameMap.get("brandid");
		}
		if (parameMap.get("autding") != null&&!"".equals(parameMap.get("autding"))) {
			str += " and t.type =" +parameMap.get("autding");
		}
		if (parameMap.get("userid") != null&&!"".equals(parameMap.get("userid"))) {
			str += " and t.userId =" +parameMap.get("userid");
		}
		str += " order by joinDate desc ";
		if (parameMap.get("page") != null && parameMap.get("row") != null) {
			int start = Integer.parseInt(parameMap.get("page")) - 1;
			int limit = Integer.parseInt(parameMap.get("row"));
			str += " limit " + start * limit + "," + limit;
		}
		List<TgBrandMember> list = dao.findList(str);
		if (list != null && !list.isEmpty()) {
			List<TgBrandMember> newlist = new ArrayList<TgBrandMember>();
			for (TgBrandMember tgBrandMember : list) {
				tgBrandMember.setCreatTimeStr(DateTimeUtil.switchDateToString(
						tgBrandMember.getJoinDate(), "yyyy-MM-dd HH:mm:ss"));
				newlist.add(tgBrandMember);
			}
			return newlist;
		}
		return null;
	}

	/**
	 * 删除信息
	 * 
	 * @param ids
	 * @return
	 */
	public boolean deleteById(String ids) {
		boolean isFlag = true;
		if ("".equals(ids))
			return false;
		String sql = "";
		String[] delteid = ids.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete  from t_g_brand_member where  id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	/**
	 * 查询总数
	 * 
	 * @param parameMap
	 * @return
	 */
	public int count(Map<String, String> parameMap) {
		try {
			String str = " SELECT t.id as id, t.brandId AS brandId , t.email AS email ,t.name AS name, t.telephoe AS telephoe,t.tel AS tel , t.joinDate AS joinDate, s.name AS code,t.type as type,g.name as qq,s.sex sex FROM  t_g_brand_member t,t_g_goods g,t_g_brand s where  t.brandId=g.id and g.brandId = s.id  ";
			if (parameMap.get("brandid") != null) {
				str += " and t.brandId =" + parameMap.get("brandid");
			}
			if (parameMap.get("autding") != null) {
				str += " and t.type =" +parameMap.get("autding");
			}
			if (parameMap.get("userid") != null) {
				str += " and t.userId =" +parameMap.get("userid");
			}
			List<TgBrandMember> list = dao.findList(str);
			if (list != null) {
				return list.size();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 0;
	}

	/**
	 * 查询某个信息
	 * 
	 * @param id
	 * @return
	 */
	public TgBrandMember findById(String id) {
		String str = " SELECT  * FROM t_g_brand_member  WHERE id =? ";
		try {
			List<TgBrandMember> list = dao.findList(str, new Object[] { id });
			if (list != null && !list.isEmpty()) {
				return list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return null;
	}

	/**
	 * 审核id
	 */
	public boolean updateAutingId(String id, String autding) {
		try {
			String sql = " UPDATE  t_g_brand_member t  set t.type=" + autding
					+ " WHERE t.id= " + id;
			dao.update(sql);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean saveOrUpdate(TgBrandMember tp) {
		try {
			dao.saveOrUpdate(tp);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据brandId查找出所有当月参加拼购人数
	 */
	 public  int    searchCountByBrandId(String brandId){
		 	try{
	    		  String  sql= " SELECT * FROM t_g_brand_member WHERE brandId =? AND DATE_FORMAT(joinDate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')";
				  List list= dao.findList(sql, new Object[]{brandId});
				  if(list!=null)return list.size();
				  return 0;
			 }catch (Exception e) {
				 e.printStackTrace(); 
				  return 0;
			}
	 }





	public int count1(Map<String, String> parameMap) {
		try {
			String str = "SELECT count(t.id) "+
					" FROM  t_g_brand_member t INNER JOIN t_g_goods g ON t.brandid = g.id "+
					" INNER JOIN t_g_category c ON  c.id = g.categoryid"+
					" LEFT JOIN t_s_member m ON m.sysuserId=t.userId where 1=1 ";
				if (parameMap.get("autding") != null&&!"".equals(parameMap.get("autding")) ) {
					if("00".equals(parameMap.get("autding")))
						str += " and t.userId = 0 ";
					else if("000".equals(parameMap.get("autding")))
						str += " and t.type =" +parameMap.get("autding");
					else
						str += " and t.userId != 0 and t.type =" +parameMap.get("autding");
				}
				if (parameMap.get("goodId") != null&&!"".equals(parameMap.get("goodId")) ) {
				str += " and t.goodId =" +parameMap.get("goodId");
				}
				if (parameMap.get("brandid") != null&&!"".equals(parameMap.get("brandid")) ) {
				str += " and t.brandId =" + parameMap.get("brandid");
				}
				if (parameMap.get("userid") != null&&!"".equals(parameMap.get("userid"))) {
				str += " and t.userId =" +parameMap.get("userid");
				}
				if (!"".equals(parameMap.get("categoryId"))&&!"0".equals(parameMap.get("categoryId"))&& parameMap.get("categoryId") != null) {
				str += " and c.id =" + parameMap.get("categoryId");
				}else if (!"".equals(parameMap.get("bigCategory"))&&!"0".equals(parameMap.get("bigCategory"))&& parameMap.get("bigCategory") != null) {
				str += " and c.pid =" + parameMap.get("bigCategory");
				}
				if (!"".equals(parameMap.get("createTime_startTime"))&& parameMap.get("createTime_startTime") != null)
				if(!"".equals(parameMap.get("createTime_endTime"))&& parameMap.get("createTime_endTime") != null)
					str += "  AND CAST(t.createTime AS DATETIME) BETWEEN CAST('"+parameMap.get("createTime_startTime")+"' AS DATETIME) AND CAST('"+parameMap.get("createTime_endTime")+"' AS DATETIME)";
				else
					str += "  and t.createTime>='"+parameMap.get("createTime_startTime")+"'";
				else if(!"".equals(parameMap.get("createTime_endTime"))&& parameMap.get("createTime_endTime") != null)
				str += "  and t.createTime<='"+parameMap.get("createTime_endTime")+"'";
				
				if (!"".equals(parameMap.get("partTime_startTime"))&& parameMap.get("partTime_startTime") != null)
				if(!"".equals(parameMap.get("partTime_endTime")))
					str += "  AND CAST(t.joinDate AS DATETIME) BETWEEN CAST('"+parameMap.get("partTime_startTime")+"' AS DATETIME) AND CAST('"+parameMap.get("partTime_endTime")+"' AS DATETIME)";
				else
					str += "  and t.joinDate>='"+parameMap.get("partTime_startTime")+"'";
				else if(!"".equals(parameMap.get("partTime_endTime"))&& parameMap.get("partTime_endTime") != null)
				str += "  and t.joinDate<='"+parameMap.get("partTime_endTime")+"'";
				List list = dao.findObjectArryList(str);
				if(null!=list&&list.size()>0){
					Object[] obj = (Object[]) list.get(0);
					return new Integer(obj[0].toString());
				}
				return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}





	public List<TgBrandMember> findByParameMap1(Map<String, String> parameMap) {
		String str = "SELECT t.id AS id, t.brandId AS brandId , t.email AS email , "+
						"t.name AS NAME, t.telephoe AS telephoe,t.tel AS tel, m.sex sex , "+
						"t.joinDate AS joinDate, g.name AS CODE,t.type AS TYPE ,t.userId,t.createTime "+
						" FROM  t_g_brand_member t INNER JOIN t_g_goods g ON t.brandid = g.id "+
						" INNER JOIN t_g_category c ON  c.id = g.categoryid"+
						" LEFT JOIN t_s_member m ON m.sysuserId=t.userId where 1=1 ";
		if (parameMap.get("autding") != null&&!"".equals(parameMap.get("autding")) ) {
			if("00".equals(parameMap.get("autding")))
				str += " and t.userId = 0 ";
			else if("000".equals(parameMap.get("autding")))
				str += " and t.type =" +parameMap.get("autding");
			else
				str += " and t.userId != 0 and t.type =" +parameMap.get("autding");
		}
		if (parameMap.get("goodId") != null&&!"".equals(parameMap.get("goodId")) ) {
			str += " and t.goodId =" +parameMap.get("goodId");
		}
		if (parameMap.get("brandid") != null&&!"".equals(parameMap.get("brandid")) ) {
			str += " and t.brandId =" + parameMap.get("brandid");
		}
		if (parameMap.get("userid") != null&&!"".equals(parameMap.get("userid"))) {
			str += " and t.userId =" +parameMap.get("userid");
		}
		if (!"".equals(parameMap.get("categoryId"))&&!"0".equals(parameMap.get("categoryId"))&& parameMap.get("categoryId") != null) {
			str += " and c.id =" + parameMap.get("categoryId");
		}else if (!"".equals(parameMap.get("bigCategory"))&&!"0".equals(parameMap.get("bigCategory"))&& parameMap.get("bigCategory") != null) {
			str += " and c.pid =" + parameMap.get("bigCategory");
		}
		if (!"".equals(parameMap.get("createTime_startTime"))&& parameMap.get("createTime_startTime") != null)
			if(!"".equals(parameMap.get("createTime_endTime"))&& parameMap.get("createTime_endTime") != null)
				str += "  AND CAST(t.createTime AS DATETIME) BETWEEN CAST('"+parameMap.get("createTime_startTime")+"' AS DATETIME) AND CAST('"+parameMap.get("createTime_endTime")+"' AS DATETIME)";
			else
				str += "  and t.createTime>='"+parameMap.get("createTime_startTime")+"'";
		else if(!"".equals(parameMap.get("createTime_endTime"))&& parameMap.get("createTime_endTime") != null)
			str += "  and t.createTime<='"+parameMap.get("createTime_endTime")+"'";
		
		if (!"".equals(parameMap.get("partTime_startTime"))&& parameMap.get("partTime_startTime") != null)
			if(!"".equals(parameMap.get("partTime_endTime")))
				str += "  AND CAST(t.joinDate AS DATETIME) BETWEEN CAST('"+parameMap.get("partTime_startTime")+"' AS DATETIME) AND CAST('"+parameMap.get("partTime_endTime")+"' AS DATETIME)";
			else
				str += "  and t.joinDate>='"+parameMap.get("partTime_startTime")+"'";
		else if(!"".equals(parameMap.get("partTime_endTime"))&& parameMap.get("partTime_endTime") != null)
			str += "  and t.joinDate<='"+parameMap.get("partTime_endTime")+"'";
		str += " order by t.createTime desc ";
		if (parameMap.get("page") != null && parameMap.get("row") != null) {
			int start = Integer.parseInt(parameMap.get("page")) - 1;
			int limit = Integer.parseInt(parameMap.get("row"));
			str += " limit " + start * limit + "," + limit;
		}
		List<TgBrandMember> list = dao.findList(str);
		if (list != null && !list.isEmpty()) {
			List<TgBrandMember> newlist = new ArrayList<TgBrandMember>();
			for (TgBrandMember tgBrandMember : list) {
				tgBrandMember.setCreatTimeStr(DateTimeUtil.switchDateToString(
						tgBrandMember.getJoinDate(), "yyyy-MM"));
				tgBrandMember.setCreatTimeString(DateTimeUtil.switchDateToString(
						tgBrandMember.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				newlist.add(tgBrandMember);
			}
			return newlist;
		}
		return null;
	}


	public List<TgBrandMember> loadByTime(String time) {
		String sql = "SELECT * FROM t_g_brand_member WHERE createTime > '"+time+"'";
		return dao.findList(sql);
	}
}
